<?php

namespace App\Models\Shop;

use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserWalletModel;
use App\Models\User\AccountFlowModel;
use DC\BonusCenter\Services\ShoppingBonus;
use Illuminate\Support\Facades\DB;
use Frontend\Models\UserBaseModel;

class UserAchievementModel extends BaseModel
{
    protected $table = 'user_achievement';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    public $dates = [
        'created_at',
        'updated_at',
    ];


    public function ApplyUser()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','user_id','member_id');
    }

    public function addUserAcInfo($user_id,$rec_num=0,$team_num=0,$rec_achievement=0,$team_achievement=0,$team_min_achievement=0){

        $count_date= Carbon::today()->timestamp;

        $data=array(
            'user_id'=>$user_id,
            'created_at'=>$count_date
        );

        $where['user_id'] = $user_id;
        $where['created_at'] = $count_date;

        $data['rec_num']=DB::raw('IFNULL(`rec_num`,0)+'.$rec_num);

        $data['team_num']=DB::raw('IFNULL(`team_num`,0)+'.$team_num);

        $data['rec_achievement']=DB::raw('IFNULL(`rec_achievement`,0)+'.$rec_achievement);

        $data['team_achievement']=DB::raw('IFNULL(`team_achievement`,0)+'.$team_achievement);

        $data['team_min_achievement']=DB::raw('IFNULL(`team_min_achievement`,0)+'.$team_min_achievement);

        return self::updateOrInsert($where,$data);
    }
}
